package com.javasm.crm.controller.aftersale;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javasm.crm.common.http.AxiosResult;
import com.javasm.crm.controller.base.BaseController;
import com.javasm.crm.domin.criteria.aftesale.RefacOutApplyCriteria;
import com.javasm.crm.domin.entity.aftersale.RefacOutApply;
import com.javasm.crm.domin.vo.aftersale.RefacOutApplyVO;
import com.javasm.crm.service.aftersale.IRefacOutApplyService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lirui
 * @since 2021-07-11
 */
@RestController
@RequestMapping("/crm/refacoutapply")
public class RefacOutApplyController extends BaseController {


    @Resource
    private IRefacOutApplyService outApplyService;

    /**
     * 分页条件查询 出库申请表
     *
     * @param criteria 条件查询构造
     * @return 查询结果
     */
    @GetMapping("find")
    @ApiOperation(value = "查询出库申请单", notes = "条件、分页均可")
    public AxiosResult find(RefacOutApplyCriteria criteria) {

        Page<RefacOutApply> page = outApplyService.getPage(criteria);

        return AxiosResult.success(page);
    }

    /**
     * 获取当前登录的用户（暂时直接返回Ray）
     *
     * @return 当前登录的用户
     */
    @GetMapping("getUser")
    @ApiOperation(value = "获得当前登录用户名", notes = "获取当前登录用户名")
    public AxiosResult getUser(HttpServletRequest request) {
//        Object loginUser = request.getSession().getAttribute("loginUser");
//        String userName = loginUser.getUserName;
        return AxiosResult.success("Ray");
    }

    @GetMapping("getOutApplyById")
    @ApiOperation(value = "根据id获取出库申请单", notes = "根据id获取出库申请单")
    public AxiosResult getOutApplyById(Integer id) {

        RefacOutApply outApply = outApplyService.getById(id);

        System.out.println(outApply.toString());

        return AxiosResult.success(outApply);
    }

    @PostMapping("saveNewOutApply")
    @ApiOperation(value = "添加申请单", notes = "包括其对应的商品list")
    public AxiosResult saveNewOutApply(@RequestBody RefacOutApplyVO refacOutApplyVO) {

        boolean flag = outApplyService.saveNewOutApply(refacOutApplyVO);

        String message = flag ? "添加成功！" : "添加失败！";

        return flag ? AxiosResult.success(message) : AxiosResult.error(message, null);
    }

    @PostMapping("alterOutApply")
    @ApiOperation(value = "修改出库申请单", notes = "根据id修改，同时修改其对应的商品表")
    public AxiosResult alterOutApply(@RequestBody RefacOutApplyVO refacOutApplyVO) {

        boolean flag = outApplyService.alterOutApply(refacOutApplyVO);

        String message = flag ? "修改成功！" : "修改失败！";

        return flag ? AxiosResult.success(message) : AxiosResult.error(message, null);
    }

    @DeleteMapping("batchDelApply")
    @ApiOperation(value = "删除出库申请单", notes = "根据id删除，同时修改其对应的商品表及残品库")
    public AxiosResult batchDelApply(@RequestBody List<Serializable> idList){

        boolean flag = outApplyService.batchDelApply(idList);

        String message = flag ? "删除成功！" : "删除失败！";

        return flag ? AxiosResult.success(message) : AxiosResult.error(message, null);
    }

    @PostMapping("approveOutApply")
    @ApiOperation(value = "审批后申请表", notes = "根据审批结果，对数据库进行跟你更新，同时生成出库订单")
    public AxiosResult approveApply(@RequestBody RefacOutApply refacOutApply, HttpServletRequest request){

//        Object approver = request.getSession().getAttribute("loginUser");

        String approver = "approver";

        boolean flag = outApplyService.approveApply(refacOutApply, approver);

        String message = flag ? "审批成功！" : "审批失败！";

        return flag ? AxiosResult.success(message) : AxiosResult.error(message, null);
    }

}
